package com.common.json;

import com.entities.EventLibrary;
import com.entities.FAQ;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class FAQJson
{

    /**
     * @EventLibraryJson.convertEventLibraryDataToJSONObject : create message
     * JsonObject type from EventLibrary object
     * @param obj
     * @return JSONObject
     */
    public static JSONObject convertFAQDataToJsonObject(FAQ obj) {
        JSONObject message = new JSONObject();
        message.put("faqId", obj.getFaqId());
        message.put("question", obj.getQuestion());
        message.put("answer", obj.getAnswer());
        return message;
    }

    /**
     * @FAQJson.convertJSONObjectStringToEventLibrary
     * @param jsonstr
     * @return FAQ
     */
    public static FAQ convertJSONObjectStringToFAQ(String jsonstr) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonstr);
            String idstr = json.get("faqId").toString();
            int id = Integer.parseInt(idstr);
            String question = json.get("question").toString();
            String answer = json.get("answer").toString();    
            FAQ faq = new FAQ(id, question, answer);
            return faq;
        } catch (ParseException ex) {
            Logger.getLogger(EventLibraryJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
   
}
